package com.txf.simple.ui.view;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.txf.simple.R;
import com.txf.simple.utils.UIUtils;

/**
 * @author txf
 * @Title
 * @package com.txf.simple.ui.view
 * @date 2016/11/25 0025
 */

public class LoadingView extends RelativeLayout {
    private ImageView loadingImg;
    private Animator animator;

    public LoadingView(Context context) {
        super(context);
    }

    public LoadingView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        addView(buildImageView(), buildImageViewLayoutParams());
        addView(buildTextView(), buildTextViewLayoutParams());
        loadingImg = (ImageView) findViewById(R.id.loading_imageview_id);
        animator = buildObjectAnimator(0, 360f);
        animator.start();
    }
    private ImageView buildImageView() {
        ImageView view = new ImageView(getContext());
        view.setId(R.id.loading_imageview_id);
        view.setBackground(setLoadingImag());
        return view;
    }

    public Drawable setLoadingImag() {
        return ContextCompat.getDrawable(getContext(), R.drawable.loading2);
    }

    private RelativeLayout.LayoutParams buildImageViewLayoutParams() {
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(UIUtils.dp2px(70), UIUtils.dp2px(70));
        lp.addRule(CENTER_IN_PARENT);
        return lp;
    }

    private TextView buildTextView() {
        TextView view = new TextView(getContext());
        view.setId(R.id.loading_textview_id);
        view.setText(setLoadingText());
        view.setTextColor(setLoadingTextColor());
        view.setTextSize(setLoadingTextSize());
        return view;
    }

    public int setLoadingTextColor() {
        return 0xFF151515;
    }

    public float setLoadingTextSize() {
        return 14;
    }

    public String setLoadingText() {
        return "加载中...";
    }

    private RelativeLayout.LayoutParams buildTextViewLayoutParams() {
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.addRule(BELOW, R.id.loading_imageview_id);
        lp.addRule(CENTER_HORIZONTAL);
        lp.setMargins(0,UIUtils.dp2px(10),0,0);
        return lp;
    }

    private Animator buildObjectAnimator(float start, float end) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(loadingImg, View.ROTATION, start, end);
        animator.setDuration(2000);
        animator.setRepeatCount(Integer.MAX_VALUE);
        animator.setInterpolator(new LinearInterpolator());
        return animator;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (animator != null) {
            animator.removeAllListeners();
            animator.cancel();
            animator = null;
        }
    }
}
